<?php
error_reporting(E_ALL);
ini_set("display_errors", true);
//$debug = true;
include_once "appinclude.php";

$curr='create';
include 'tabs.php';

function addRide2($uid, $zip, $description, $seats, $depart_time){
	$bumConn = getConnection();
	$uid = mysql_real_escape_string($uid, $bumConn);
	$zip = mysql_real_escape_string($zip, $bumConn);
	$description = mysql_real_escape_string($description, $bumConn);
	$seats = mysql_real_escape_string($seats, $bumConn);
	$depart_time = mysql_real_escape_string($depart_time, $bumConn);
	//Error Checking
	$success = true;
	$error = "<div class='errorbox'><h2>Error</h2>";
	if($uid == ""){
		$success = false;
		$error = $error."There was a problem retrieving your user ID.<br /><br />";
	}
	if($zip == ""){
		$success = false;
		$error = $error."You must enter a destination ZIP.<br /><br />";
	}
	if($description == ""){
		$success = false;
		$error = $error."You must enter a description.<br /><br />";
	}
	if($seats <= 0){
		$success = false;
		$error = $error."You must enter an amount of seats.";
	}
	$error = $error."</div>";
	if($success){
		$ride = "INSERT INTO RidesAvailable (uid, destination_zip, description, seats_available, departure_time, updated_at) VALUES"
			. "('$uid', '$zip', '$description', '$seats', '$depart_time', NOW())";	
		mysql_query($ride, $bumConn);		
		mysql_close();
		$location = GetNameFromZip($zip);
		$content = "<div class='infobox><h1>You successfully submitted a ride to $location!</h1><br />".
			"Your ride has $seats available seats<br />".
			"It is scheduled for $depart_time<br />".
			"</div>";
		return array(true, $content, $zip, $location, $description);
	}
	else {
		return array(false, $error, null, null, null);
	}
}

if(isset($_POST["submitRide"])) {
	$departure_time = "$_POST[year]-$_POST[month]-$_POST[day] $_POST[time]:00:00";
	list($result, $content, $zip, $location, $description) = addRide2($facebook->user, $_POST['destination_zip'], $_POST['description'], $_POST['seats'], $departure_time);
	if($result){
		$picture = APP_CALLBACK_URL . "images/hummer.jpg";
		$attachment = array(
			'name' => "I am driving to $location and I have room in my car", 
			'href' => APP_CANVAS_URL, 
			'description' => "Whether it's the global climate or the financial climate that drives your desicions. One thing is for sure. Now is a time to drive less. There is no reason for both of us to drive to $location. So why dont you just join me.", 
			'media' => array(array(
				'type' => 'image', 
				'src' => $picture, 
				'href' => APP_CANVAS_URL
			)),
			'properties' => array(
				'Destination' => array('text' => $location, 'href' => APP_CANVAS_URL),
				'Seats Available' => array('text' => $_POST['seats'], 'href' => APP_CANVAS_URL),
				'We are leaving' => array('text' => date('M d Y \\a\\t h:ia',strtotime($departure_time)), 'href' => APP_CANVAS_URL)
			)
		);
		$attachment = json_encode($attachment);
		print_debug($attachment);
		try{
			$error = $facebook->api_client->stream_publish($description, $attachment);
		}
		catch(Exception $ex){
			print_debug($ex);
		};
	}
	echo $content;
}

if($facebook->api_client->users_hasAppPermission('publish_stream', $facebook->user) == "0"){
	echo "<script type='javascript'>Facebook.showPermissionDialog('publish_stream');</script>";
}

?>

<link rel="stylesheet" type="text/css" href="<?=APP_CALLBACK_URL?>typeahead.css" />
<script type="text/javascript" src="<?=APP_CALLBACK_URL?>typeahead.js?2"></script>

<form method='POST' enctype='multipart/form-data' action='createARide.php?action=post'>
	<table class='submittable' style='width:400px;margin:20px 0px 0px;'>
		<tr style='margin-top:10px;'>
			<td colspan='4'>Destination ZIP:</td>
		</tr>
		<tr>
			<td colspan='4'><input class='textInput' name='destination_zip' id="destination_zip" size='40' /></td>
		</tr>
		<tr style='margin-top:10px;'>
			<td colspan='4'>When are you going?</td>
		</tr>
		<tr>
			<td style='width:100px;'>
				<select name="month">
				<?php  $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
					$i = 1;
					foreach($months as &$month){
						if($i < 10)
							$j = "0$i";
						else
							$j = $i;
						echo "<option value=$j>$month</option>\n";
						$i++;
					}				
				?>
				</select>
			</td>
			<td style='width:50px;'>
				<select name="day">
				<?php  
					for($i = 1; $i <= 31; $i++){
						if($i < 10)
							$j = "0$i";
						else
							$j = $i;
						echo "<option value=$j>$i</option>\n";
					}
				?>
				</select>
			</td>
			<td style='width:75px;'>
				<select name="year">
				<?php  
					for($i = 2010; $i <= 2012; $i++){
						echo "<option value=$i>$i</option>\n";
					}
				?>
				</select>
			</td>
			<td style='width:120px;'>
				<select name="time">
				<?php  
					for($i = 0; $i <= 23; $i++){
						if($i < 10)
							$j = "0$i";
						else
							$j = $i;
						echo "<option value=$j>$i:00</option>\n";
					}
				?>
				</select>
			</td>
		</tr>
		<tr style='margin-top:10px;'>
			<td colspan='4'>How many seats are available?</td>
		</tr>
		<tr>
			<td colspan='4'><input class='textInput' name='seats' size='5' maxlength='3' /></td>
		</tr>
		<tr style='margin-top:10px;'>
			<td colspan='4'>Any additional information?</td>
		</tr>
		<tr>
			<td colspan='4'><input class='textInput' name='description' /></td>
		</tr>
		<tr>
			<td  colspan='4'><input class='inputbutton' type='submit' name='submitRide' value='Submit a Trip' /></td>
		</tr>
	</table>
</form>

<script> 
options = {
		preMsgTxt: "Search for a ride",	// text to display when nothing has been typed
		menuOpacity: 90, 					// opacity of the menu
		ajaxUrl: "http://www2.newpaltz.edu/~plotkin2/bumaride/locationService.php",	// url to your data source, must be absolute URL
		focus: false,						// whether or not to auto-focus the textbox upon creation
		onEnter: function(event) {},
		delayTime: 700,						// amount of idle time after a keypress before making the ajax call
		clearOnEnter: false					// whether or not to clear the text after they hit enter
	};

var suggestr = new ajaxSuggestFbml(document.getElementById('destination_zip'),options);
</script>
